package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NursingProjectPlanServiceImpl implements NursingProjectPlanService {
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    //删除某个计划下的所有项目
    @Override
    public void deletcByPlanId(Long id) {
        nursingProjectPlanMapper.deletcByPlanId(id);
    }

    //批量更新plan表中的项目
    @Override
    public void update(List<NursingProjectPlanDto> projectPlans) {
        //将DTO集合遍历转为实体类
        List<NursingProjectPlan> nursingProjectPlans = new ArrayList<>();
        //创建一个新的实体类对象用来遍历的时候每次存储对象
        NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
        for (NursingProjectPlanDto nursingProjectPlanDto : projectPlans) {
            //把DTO对象拷贝到实体类对象中
            BeanUtils.copyProperties(nursingProjectPlanDto,nursingProjectPlan);
            //将实体类添加到实体类集合中
            nursingProjectPlans.add(nursingProjectPlan);
        }
        nursingProjectPlanMapper.updates(nursingProjectPlans);

    }

    @Override
    public void adds(List<NursingProjectPlanDto> projectPlans) {
        //将DTO集合遍历转为实体类
        List<NursingProjectPlan> nursingProjectPlans = new ArrayList<>();
        //创建一个新的实体类对象用来遍历的时候每次存储对象
        NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
        for (NursingProjectPlanDto nursingProjectPlanDto : projectPlans) {
            //把DTO对象拷贝到实体类对象中
            BeanUtils.copyProperties(nursingProjectPlanDto,nursingProjectPlan);
            //将实体类添加到实体类集合中
            nursingProjectPlans.add(nursingProjectPlan);
        }
        nursingProjectPlanMapper.adds(nursingProjectPlans);
    }
}
